<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';

$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;

if($_RQ['op']=='main'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$sum = pdo_count('jy_xiuvote_opus',$params);
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.$_RQ['keyword'].'%';
			$return['params']['title']=$_RQ['keyword'];
		}
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'rid'=>$postdata['rid'],
			'title'=>$postdata['title'],
			'thumb'=>$postdata['thumb'],
			'isshow'=>$postdata['isshow'],
			'description'=>$postdata['description'],
			'opus_images'=>$postdata['opus_images'],
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_xiuvote_opus',$insert);
		exi('上传作品成功','success');
	}
	$member=pdo_get('jy_xiuvote_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['member']=$member;
	
	set_html('title','作品列表');
	$return['sum']=$sum;
	exi($return);

}
if($_RQ['op']=='getlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
			$list=pdo_getall('jy_xiuvote_opus',$params,$fields,'','likes desc, id desc');
		}else{
			//$params['title like']='%'.$_RQ['keyword'].'%';
			
			$return['params']['title']=$_RQ['keyword'];
			$list=pdo_getall('jy_xiuvote_opus',$params,$fields,'','likes desc, id desc');
			if($list){
				$sum=count($list);
				foreach($list as $k=>&$v){
					$title=base64_decode($v['title']);
					
					if(!strexists($title,$_RQ['keyword'])){
						
						unset($list[$k]);
					}
					
				}
			}
			$list=array_values($list);
		}
	}else{
			$pindex = max(1, intval($_RQ['page']));
			$psize = 10;
			$list=pdo_getslice('jy_xiuvote_opus',$params,array($pindex,$psize),$total,$fields,'','id desc');
	}

	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['opus_images']=json_decode($item['opus_images'],true);
			$item['picsnum']=count($item['opus_images']);
			$item['votenums']=pdo_count('jy_xiuvote_votelog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'is_shenhe'=>1));
			$item['group']=pdo_get('jy_xiuvote_groups',array('uniacid'=>$_SESSION['uniacid'],'id'=>$reply['groupid']));
			$jifen=pdo_getsum('jy_xiuvote_sendlplog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'jifen');
			$item['votenums']=$item['votenums']+$jifen;
			$item['likes']=$item['likes']+$jifen;
			//$item['statustitle']=$item['status']==0?'待审核':$item['status']==1?'审核通过':'不通过';
			if($item['status']==0){
				$item['statustitle']='审核中';
			}elseif($item['status']==1){
				$item['statustitle']='审核通过';
			}else{
				$item['statustitle']='不通过';
			}
		}
	}
	else{
		$sum=0;
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id = intval($_RQ['id']);
	$item=pdo_get('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['opus_images']=json_decode($item['opus_images'],true);
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['title']){
			exi('作品标题不能为空！','error');
		}
		if(!$postdata['description']){
			exi('作品描述不能为空！','error');
		}
		if(!$postdata['opus_images']){
			exi('作品图片不能为空！','error');
		}
		if(mb_strwidth($postdata['title'])>40){
			exi('标题不能超过20个字！','error');
		}
		if(mb_strwidth($postdata['description'])>400){
			exi('作品描述不能超过100个字！','error');
		}
		$insert=array(
			'rid'=>$postdata['rid'],
			'title'=>$postdata['title'],
			'thumb'=>$postdata['opus_images']['0'],
			'description'=>$postdata['description'],
			'isshow'=>$postdata['isshow'],
			'status'=>0,
			'opus_images'=>ijson_encode($postdata['opus_images']),
		);
		pdo_update('jy_xiuvote_opus',$insert,array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
		exi('更新成功','success','jy_xiuvote/myopus/main');
	}
	$return['post']=$item;
	exi($return);
}
if($_RQ['op']=='del'){
	
	if(!$_RQ['id']){
		exi('作品不存在哦！','error');
	}
	
	pdo_delete('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！');

}

if($_RQ['op']=='open'){
	$postdata=get_postdata();
	$id=$postdata['id'];
	if(!$id){
		exi('作品不存在哦！','error');
	}
	$isshow=pdo_getcolumn('jy_xiuvote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'isshow');
	if($isshow){
		pdo_update('jy_xiuvote_opus',array('isshow'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		pdo_update('jy_xiuvote_opus',array('isshow'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	exi('操作成功');
}